﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Finder.Properties;
using Finder.Utilities.KeyCapture;

namespace Finder
{
    /// <summary>
    /// Interaction logic for Config.xaml
    /// </summary>
    public partial class Config : Window
    {
        /// <summary>
        /// Key stack for the app.
        /// </summary>
        private readonly KeyIdentifier[] keyStack = new KeyIdentifier[2];

        /// <summary>
        /// Initializes a new instance of the <see cref="Config"/> class.
        /// </summary>
        public Config()
        {
            InitializeComponent();

            this.HotKeyCaptureBackground = this.btnHotKeyCapture.Background;

            if (Settings.Default.EmailToOutlook)
            {
                RbEmailToOutlook.IsChecked = true;
            }
            else
            {
                RbNameToOutlook.IsChecked = true;
            }
            
            btnHotKeyCapture.Content = Settings.Default.HotKey;

            this.Kh = new KeyboardHook(KeyboardHook.Parameters.PassAllKeysToNextApp);
            this.Kh.KeyIntercepted += this.KhKeyIntercepted;
        }

        /// <summary>
        /// Gets or sets the hot key capture background.
        /// </summary>
        /// <value>The hot key capture background.</value>
        protected Brush HotKeyCaptureBackground { get; set; }

        /// <summary>
        /// Khes the key intercepted.
        /// </summary>
        /// <param name="e">The <see cref="KeyboardHook.KeyboardHookEventArgs"/> instance containing the event data.</param>
        private void KhKeyIntercepted(KeyboardHook.KeyboardHookEventArgs e)
        {
            if (this.keyStack[0] == null)
            {
                this.keyStack[0] = new KeyIdentifier(e.KeyCode, e.KeyName);
            }
            else
            {
                if (this.keyStack[0].KeyCode != e.KeyCode)
                {
                    this.keyStack[1] = new KeyIdentifier(e.KeyCode, e.KeyName);
                }
            }

            if (this.Recording)
            {
                if (this.keyStack[0] != null && this.keyStack[1] != null)
                {
                    btnHotKeyCapture.Content = this.keyStack[0].KeyName +
                                               (!string.IsNullOrEmpty(this.keyStack[1].KeyName) ? " + " : string.Empty) +
                                               this.keyStack[1].KeyName;

                    this.RecordedHotKey = new KeyIdentifier[2]; 
                    this.keyStack.CopyTo(this.RecordedHotKey, 0);
                    this.Recording = false;
                }
                else
                {
                    btnHotKeyCapture.Content = "No Hot Key Assigned!";
                }
            }

            if (this.HotKeySet())
            {
                this.ClearHotKey();
            }

            btnHotKeyCapture.Background = this.HotKeyCaptureBackground;
        }

        /// <summary>
        /// Gets or sets the recorded hot key.
        /// </summary>
        /// <value>The recorded hot key.</value>
        protected KeyIdentifier[] RecordedHotKey { get; set; }

        /// <summary>
        /// Clears the hot key.
        /// </summary>
        private void ClearHotKey()
        {
            this.keyStack[0] = this.keyStack[1] = null;
        }

        /// <summary>
        /// Hots the key set.
        /// </summary>
        /// <returns>True or false.</returns>
        private bool HotKeySet()
        {
            return this.keyStack[0] != null && this.keyStack[1] != null;
        }

        /// <summary>
        /// Gets or sets the kh.
        /// </summary>
        /// <value>The kh.</value>
        protected KeyboardHook Kh { get; set; }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the 1 event of the Button_Click control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Save();
            this.Close();
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        private void Save()
        {
            try
            {
                Settings.Default.EmailToOutlook = Convert.ToBoolean(RbEmailToOutlook.IsChecked);

                if (this.RecordedHotKey[0] != null && this.RecordedHotKey[1] != null)
                {
                    Settings.Default.HotKey = this.RecordedHotKey[0].KeyName + " + " + this.RecordedHotKey[1].KeyName;
                    Settings.Default.HotKeyCode = this.RecordedHotKey[0].KeyCode + "|" + this.RecordedHotKey[1].KeyCode;
                }

                Settings.Default.Save();
            }
            catch
            {
                // Godzilla eat it!
            }
        }

        /// <summary>
        /// Handles the Click event of the btnHotKeyCapture control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnHotKeyCapture_Click(object sender, RoutedEventArgs e)
        {
            this.Recording = true;
            this.btnHotKeyCapture.Content = "Press key combination";
            this.btnHotKeyCapture.Background = Brushes.Red;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Config"/> is recording.
        /// </summary>
        /// <value><c>true</c> if recording; otherwise, <c>false</c>.</value>
        protected bool Recording { get; set; }
    }
}
